<?php

namespace catchAdmin\basics\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\basics\model\Currency as CurrencyModel;
use catcher\CatchAuth;
class Currency extends CatchController
{
    
    protected $currencyModel;
    
    /**
     *
     * @time 2022/03/01 15:00
     * @param CurrencyModel $currencyModel
     * @return mixed
     */
    public function __construct(CurrencyModel $currencyModel)
    {
        $this->currencyModel = $currencyModel;
    }
    
    /**
     *
     * @time 2022/03/01 15:00
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->currencyModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/28 14:25
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,Request $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->currencyModel->storeBy($params),"新建成功");
    }
    
    /**
     *
     * @time 2022/03/01 15:00
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $params = $request->filterEmptyField()->post();
        return CatchResponse::success($this->currencyModel->updateBy($params['id'], $params),"修改成功");
    }
    
    /**
     *
     * @time 2022/03/01 15:00
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        return CatchResponse::success($this->currencyModel->deleteBy($id),"删除成功");
    }

    /**
     * 获取全部币种信息
     * @time 2022/03/01 15:00
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function getCurrency()
    {
        $data = $this->currencyModel->where('deleted_at',0)->field('id,currency_name,currency_num')->select();
        return CatchResponse::success($data,"查询成功");
    }
}